<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png"/>
      </div>
      <!--登录表单区域-->
      <el-form ref="loginFormRef" class="login_form" :model="login_form" :rules="rules">
        <el-form-item prop="loginName">
          <el-input prefix-icon="iconfont icon-dengluyonghu"
          v-model="login_form.loginName">
        </el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input prefix-icon="iconfont icon-denglu-mima
" v-model="login_form.pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      login_form: {
        loginName: '',
        pwd: ''
      },
      // 这是表单的验证规则
      rules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 20, message: '长度必须大于5 小于20', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 20, message: '长度必须大于5 小于30', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮
    resetLoginForm () {
      // console.log(this)
      this.login_form.loginName = ''
      this.login_form.pwd = ''
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      // 表单验证
      this.$refs.loginFormRef.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.post('api/UserManagement', this.login_form)
        if (JSON.parse(res).meta.status === 200) {
          this.$msg({
            message: '登录成功',
            type: 'success'
          })
          window.sessionStorage.setItem('ticker', JSON.parse(res).data.ticket)
          setTimeout(() => {
            this.$router.push('/home')
          }, 3000)
        } else {
          this.$msg.error('登录失败')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
html, body, #app{
    height: 100%;
    margin: 0;
    padding: 0;
}
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}
.login_box{
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%) ;
}
.avatar_box{
  padding: 10px;
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position:absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: #fff;
  img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form{
  position:absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
.btns{
  display: flex;
  justify-content: right;
}
</style>
